<?php
/**
 * Module:  Data
 * About:   Модель данных
 * Author:  Vlad Andreev <php.vlad@gmail.com>
 **/

namespace Data;

class Data extends \Control\Model {
    
    public $name;
    public $title;
    public $table;
    public $module;
    public $fields = array();
    public $data;
    
    public function __construct() {
        parent::__construct();
    }
    
    public function setName($name) {
        
        $this->repDataType = $this->em->getRepository('Data\Entities\DataType');
        $this->repDataField = $this->em->getRepository('Data\Entities\DataField');
        
        $dataType = $this->repDataType->findOneBy(array('name' => $name));
        
        if (!empty($dataType)) {
            $this->name   = $name;
            $this->title  = $dataType->title;
            $this->entity = $dataType->entity;
            $this->module = $dataType->module;
        }
        
        $fields = $this->repDataField->findBy(
            array('data_name' => $this->name, 'active' => 1),
            array('num' => 'ASC')
        );
        
        foreach ($fields as $field)
            $this->fields[$field->name] = $field;
    }
    
    public function setData($data) {
        $this->data = $data;
    }
    
    // Форма добавления материала
    public function addForm($options, $footer = '', $form_params = array()) {
        
        if (is_array($options)) {
            
            if(empty($options['fields'])) {
                $fields = $this->fields;
            } else {
                $fields_ = explode($this->conf->ac_sep, $options['fields']);
                
                foreach($fields_ as $field)
                    $fields[] = $this->fields[$field];
            }
            
            $params   = (!empty($options['params'])) ? $options['params'] : array();
            $footer   = (!empty($options['footer'])) ? $options['footer'] : '';
            $title    = (!empty($options['title']))  ? $options['title']  : '';
            $redirect = (!empty($options['redirect']))  ? $options['redirect']  : 'list';
        }
        
        $form = new \Control\Forms;
        $form->setTitle($title);
        $form->setTemplate('form');
        $form->setFormId('form-'. $this->name .'-add');
        $form->setFormAction('/'. $this->module .'/save/'. $this->name);
        
        foreach ($fields as $field) {
            $field->id      = $this->module .'_'. $field->name;
            $field->caption = $field->title;
            
            $form->addObject($field);
        }
        
        if ($footer == '') {
            $form->addFooter(array(
                'type'    => 'button',
                'id'      => 'ok',
                'value'   => 'Готово',
                'action'  => "acFormSubmit('form-". $this->name ."-add', '". $redirect. "');"
            ));
            $form->addFooter(array(
                'type'    => 'button',
                'id'      => 'save',
                'value'   => 'Сохранить',
                'action'  => "acFormSubmit('form-". $this->name ."-add');"
            ));
            $form->addFooter(array(
                'type'    => 'button',
                'id'      => 'cancel',
                'value'   => 'Отмена',
                'href'    => '#'. $this->module .'/list'
            ));
        } else {
            if (is_array($footer))
                foreach ($footer as $obj)
                    $form->addFooter($obj); 
            else
                $form->addFooter($footer);
        }
        
        return $form->render();
    }
    
    // Форма редактирования материала
    public function editForm($options, $footer = '', $form_params = array()) {
        
        if (is_array($options)) {
            if(empty($options['fields'])) {
                $fields = $this->fields;
            } else {
                $fields_ = explode($this->conf->ac_sep, $options['fields']);
                
                foreach($fields_ as $field)
                    $fields[] = $this->fields[$field];
            }
            
            $params   = (!empty($options['params'])) ? $options['params'] : array();
            $header   = (!empty($options['header'])) ? $options['header'] : '';
            $footer   = (!empty($options['footer'])) ? $options['footer'] : '';
            $title    = (!empty($options['title']))  ? $options['title']  : '';
            $redirect = (!empty($options['redirect']))  ? $options['redirect']  : 'list';
            
            $deleteQuestion = (!empty($options['deleteQuestion']))  ? $options['deleteQuestion']  : 'Do you really want to delete this item?';
            
            // $buttonOk        = (!empty($options['buttonOk']))  ? $options['buttonOk']  : 'Ok';
            // $buttonSave      = (!empty($options['buttonSave']))  ? $options['buttonSave']  : 'Save';
            // $buttonCancel    = (!empty($options['buttonCancel']))  ? $options['buttonCancel']  : 'Cancel';
            // $buttonDelete    = (!empty($options['buttonDelete']))  ? $options['buttonDelete']  : 'Delete';
        }
        
        $form = new \Control\Forms;
        $form->setTitle($title);
        $form->setTemplate('form');
        $form->setFormId('form-'. $this->name .'-edit');
        $form->setFormAction('/'. $this->module .'/save/'. $this->name .'/'. $this->data->id);
        
        if (!empty($header))
            $form->addHTML($header);
        
        foreach ($fields as $field) {
            $field_name = $field->name;
            
            $field->id      = $this->module .'_'. $field->name;
            $field->value   = ($field->type != 'password') ? $this->data->$field_name : '';
            $field->caption = $field->title;
            
            $form->addObject($field);
        }
        
        if ($footer == '') {
            $form->addFooter(array(
                'type'    => 'button',
                'id'      => 'ok',
                'value'   => 'Готово',
                'action'  => "acFormSubmit('form-". $this->name ."-edit', '". $redirect ."');" ));
            $form->addFooter(array(
                'type'    => 'button',
                'id'      => 'save',
                'value'   => 'Сохранить',
                'action'  => "acFormSubmit('form-". $this->name ."-edit');" ));
            $form->addFooter(array(
                'type'    => 'button',
                'id'      => 'delete',
                'value'   => 'Удалить',
                'action'  => "javascript: if (MsgOkCancel('". $deleteQuestion ."')) { acDelete('{$this->name}', '". $this->data->id ."', '1', '". $redirect ."'); }" ));
            $form->addFooter(array(
                'type'    => 'button',
                'id'      => 'cancel',
                'value'   => 'Отмена',
                'href'    => '#'. $this->module .'/list' ));
        } else {
            if (is_array($footer))
                foreach ($footer as $foot)
                    $form->addFooter($foot);
            else
                $form->addFooter($footer);
        }
        
        return $form->render();
    }
    
    public function getTitle() {
        return $this->title;
    }
    
    public function getTable() {
        return $this->table;
    }
        
    public function getModule() {
        return $this->module;
    }
    
    public function getField($field_name) {
        return $this->fields[$field_name];
    }
}